<?php namespace App\Repositories;

use Dingo\Api\Exception\ResourceException;
use Gregwar\Captcha\CaptchaBuilder;
use House;

class SendSmsRepository implements HouseRepositoryInterface {

    public function key()
    {
        return 2;

        $addMinutes = config('app.phone_addMinutes');

        if (config('app.env') == 'local') {
            $code = '123456';
        }else {
            // todo 发送验证码

        }
        $key = 'phone-'.str_random(15);

        $payload = \json_decode(request()->payload, true);

        if(array_get($payload, 'payload.phone') != request()->phone) {
            throw new ResourceException('error',[['error_code'=>'430005','message'=>'手機號碼非法操作！']]);
        }
        $expiredAt = now()->addMinutes($addMinutes);

        \Cache::put($key, ['payload'=>$payload, 'code' => $code], $expiredAt);

        $result = [
            'phone_key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
        ];
        return $result;
    }
}